package com.myzhouye.business.controller;/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

import com.alibaba.fastjson.JSONObject;
import com.myzhouye.business.bean.R;
import com.myzhouye.business.service.PushService;
import com.myzhouye.business.sse.server.SseConstants;
import com.myzhouye.business.sse.server.SseServer;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @program: maple-main
 * @description:
 * @author: 那条蠢鱼
 * @create: 2024-01-03 13:04
 **/
@RestController
@RequestMapping("push")
@RequiredArgsConstructor
public class PushController {


    private final SseServer sseServer;
    private final PushService pushService;

    @GetMapping("{groupId}")
    public R pushData(@PathVariable String groupId,String msg){
        sseServer.batchAllSendMessage(groupId, msg);
        return R.ok("");
    }

    @GetMapping("tag/{groupId}")
    public R pushTag(@PathVariable String groupId,String tag){
        JSONObject json=new JSONObject();
        json.put("tag",tag);
        sseServer.batchAllSendMessage(groupId, json.toJSONString());
        return R.ok("");
    }

    @PostMapping("info/{groupId}")
    public R pushInfo(@PathVariable String groupId,@RequestBody JSONObject json){
        sseServer.batchAllSendMessage(groupId, json.toJSONString());
        return R.ok("");
    }

    /**
     * 倒计时
     * @param groupId
     * @return
     */
    @PostMapping("timer/{groupId}")
    public R timer(@PathVariable String groupId,int second){
        String msg=pushService.timerMsgInit(second);
        sseServer.batchAllSendMessage(groupId, msg);
        return R.ok("");
    }

    @PostMapping("alive/{groupId}")
    public R alive(@PathVariable String groupId){
        sseServer.aliveCheck(groupId);
        return R.ok("");
    }
}
